package com.fenghongzhang.module_user.provider

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.net.Uri
import android.provider.ContactsContract.AUTHORITY
import androidx.room.Room
import com.fenghongzhang.module_user.dao.UserDao
import com.fenghongzhang.module_user.db.AppDatabase
import com.fenghongzhang.module_user.model.User

class MyContentProvider : ContentProvider() {

    private lateinit var userDao: UserDao
    private lateinit var db :AppDatabase


    companion object {
        const val AUTHORITY = "com.feng.a2207"
        val CONTENT_URI: Uri = Uri.parse("content://$AUTHORITY")
    }
    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
        TODO("Implement this to handle requests to delete one or more rows")
    }

    override fun getType(uri: Uri): String? {
        TODO(
            "Implement this to handle requests for the MIME type of the data" +
                    "at the given URI"
        )
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? {
        TODO("Implement this to handle requests to insert a new row.")
    }

    override fun onCreate(): Boolean {
        db = AppDatabase.getDatabase(context!!)
        userDao = db.userDao()
        return true
    }

    override fun query(
        uri: Uri, projection: Array<String>?, selection: String?,
        selectionArgs: Array<String>?, sortOrder: String?
    ): Cursor? {
        val all = userDao.getAll()
        return  convertUsersToCursor(all)

    }

    override fun update(
        uri: Uri, values: ContentValues?, selection: String?,
        selectionArgs: Array<String>?
    ): Int {
        TODO("Implement this to handle requests to update one or more rows.")
    }
}
